<template>
    <div class="resume-uploader">
        <h2>上传简历进行解析</h2>
        <input type="file" @change="handleFileChange" accept=".doc,.docx,.pdf" />
        <button @click="uploadResume" :disabled="!selectedFile">上传解析</button>

        <div v-if="loading">解析中，请稍候...</div>

        <div v-if="error" class="error">{{ error }}</div>

        <div v-if="result">
            <h3>解析结果：</h3>
            <pre>{{ formattedResult }}</pre>
        </div>
    </div>
</template>

<script>
import axios from "axios";

export default {
    data() {
        return {
            selectedFile: null,
            base64FileContent: "",
            result: null,
            error: "",
            loading: false,
        };
    },
    computed: {
        formattedResult() {
            return JSON.stringify(this.result, null, 2);
        },
    },
    methods: {
        handleFileChange(event) {
            this.error = "";
            this.result = null;
            const file = event.target.files[0];
            if (!file) {
                this.selectedFile = null;
                this.base64FileContent = "";
                return;
            }

            // 检查文件类型（可根据需要调整）
            const allowedTypes = [
                "application/msword",
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                "application/pdf",
            ];
            if (!allowedTypes.includes(file.type)) {
                this.error = "请上传.doc、.docx或.pdf格式的简历文件";
                this.selectedFile = null;
                return;
            }

            this.selectedFile = file;

            // 读取文件为Base64
            const reader = new FileReader();
            reader.onload = () => {
                const base64String = reader.result.split(",")[1]; // 去掉前缀 data:*/*;base64,
                this.base64FileContent = base64String;
            };
            reader.onerror = () => {
                this.error = "读取文件失败";
                this.selectedFile = null;
            };
            reader.readAsDataURL(file);
        },

        async uploadResume() {
            if (!this.selectedFile || !this.base64FileContent) {
                this.error = "请先选择有效的简历文件";
                return;
            }

            this.error = "";
            this.result = null;
            this.loading = true;

            const url = "https://resumesdk.market.alicloudapi.com/ResumeParser"; // 接口地址
            const appcode = "55eda8e822e2421499fcc94005405d82"; // TODO：替换成你自己的AppCode

            const data = {
                file_name: this.selectedFile.name,
                file_cont: this.base64FileContent,
                need_avatar: 0, // 是否需要头像，0不需要
            };

            try {
                const response = await axios.post(url, data, {
                    headers: {
                        Authorization: "APPCODE " + appcode,
                        "Content-Type": "application/json; charset=UTF-8",
                    },
                });

                if (response.status === 200) {
                    this.result = response.data;
                } else {
                    this.error = `请求失败，状态码：${response.status}`;
                }
            } catch (err) {
                this.error =
                    err.response && err.response.data
                        ? `请求错误：${JSON.stringify(err.response.data)}`
                        : `请求错误：${err.message}`;
            } finally {
                this.loading = false;
            }
        },
    },
};
</script>

<style scoped>
.resume-uploader {
    max-width: 600px;
    margin: 20px auto;
    font-family: Arial, sans-serif;
}

input[type="file"] {
    margin-bottom: 10px;
}

button {
    margin-left: 10px;
    padding: 6px 12px;
}

.error {
    margin-top: 10px;
    color: red;
}

pre {
    background: #f5f5f5;
    padding: 10px;
    overflow-x: auto;
}
</style>
